package com.lv.service.adapter

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.BaseSectionQuickAdapter
import com.lv.common.base.DataBindBaseViewHolder
import com.lv.common.imp.OnPositionTListener
import com.lv.common.ktx.LoadSrc
import com.lv.service.R
import com.lv.service.bean.CommonTab
import com.lv.service.databinding.ItemMineTabImgBinding
import com.lv.service.databinding.ItemTabHeaderBinding

/**
 * 作者： ygx
 * 创建日期：2022/6/24 8:10 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 *
 */
class MineTabAdapter(private val datas: MutableList<CommonTab>?) :
    BaseSectionQuickAdapter<CommonTab, DataBindBaseViewHolder>
        (R.layout.item_tab_header, R.layout.item_mine_tab_img, datas) {

    override fun convert(helper: DataBindBaseViewHolder, item: CommonTab) {
        val mBinding: ItemMineTabImgBinding =
            helper.getBind() as ItemMineTabImgBinding
        mBinding.itemRoot = item
//        //防止列表闪烁
        mBinding.executePendingBindings()
        val layoutParams = mBinding.tvDot.layoutParams as ConstraintLayout.LayoutParams
        if (item.image == "K") {
            mBinding.tvUnread.visibility = View.GONE
            layoutParams.width = SizeUtils.dp2px(10.0f)
            layoutParams.height = SizeUtils.dp2px(10.0f)
            layoutParams.topMargin = SizeUtils.dp2px(-2.0f)
            layoutParams.marginEnd = SizeUtils.dp2px(-4.0f)
        } else {
            layoutParams.width = SizeUtils.dp2px(18.0f)
            layoutParams.height = SizeUtils.dp2px(18.0f)
            layoutParams.topMargin = SizeUtils.dp2px(-7.0f)
            layoutParams.marginEnd = SizeUtils.dp2px(-6.0f)
            mBinding.tvUnread.visibility = View.VISIBLE
        }
        mBinding.tvDot.layoutParams = layoutParams
        val layoutPosition = helper.layoutPosition
        val clParent: ConstraintLayout = helper.getView<ConstraintLayout>(R.id.cl_containers)
        val ivImage = helper.getView<ImageView>(R.id.iv_image)
        ivImage.LoadSrc(item.tag)
        clParent.setOnClickListener {
            onViewItemClickListener?.onClick(layoutPosition, item.image)
        }

    }

    var onViewItemClickListener: OnPositionTListener<String>? = null
    override fun convertHeader(helper: DataBindBaseViewHolder, item: CommonTab) {
        val mBinding: ItemTabHeaderBinding =
            helper.getBind() as ItemTabHeaderBinding
        mBinding.itemRoot = item.name
//        //防止列表闪烁
        mBinding.executePendingBindings()
        val tvContent = helper.getView<TextView>(R.id.tv_content)

        val layoutPosition = helper.layoutPosition
        val layoutParams = tvContent.layoutParams as ConstraintLayout.LayoutParams
        if (layoutPosition == 0) {
            layoutParams.topMargin = SizeUtils.dp2px(10.0f)
        } else {
            layoutParams.topMargin = SizeUtils.dp2px(17.0f)
        }
        tvContent.layoutParams = layoutParams

    }


}